Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Prevent state mutation in fetchRevisionsPromise function #6112

Merged

Conversation

Abishekcs
Copy link
Contributor

@Abishekcs Abishekcs commented Jan 13, 2025

What this PR does

  • Fixes a state mutation issue in the fetchRevisionsPromise function that caused errors in Redux.
  • Updates the logic to create a new course object with updated revisions instead of directly modifying the existing course object.
  • Ensures Redux Toolkit's immutability rules are followed to prevent unexpected behavior.

Cause of the Mutation:

  • The course.revisions property was being directly modified within the fetchRevisionsPromise function.
  • Direct state mutation violates Redux’s immutability principles, which led to a detected mutation error.

Fix for the Mutation:

  • Replaced the direct mutation of course.revisions with the creation of a new object:
const updatedCourse = {
  ...course,
  revisions: sortRevisionsByDate(revisions),
};
  • This approach maintains immutability by ensuring the original course object remains unchanged.

Note:

For testing please set enableMutationChecks to true in create_store.js

Screenshots

Before:

Before.mp4
Before.mp4

After:

After.mp4
After.mp4

- Avoid direct mutation of `course.revisions` by creating a new object with updated revisions.
- Ensures compliance with Redux's immutability rules to avoid errors during state updates.
@ragesoss ragesoss merged commit 3b708e8 into WikiEducationFoundation:master Jan 13, 2025
1 check failed
@Abishekcs Abishekcs deleted the fix/redux-state-mutation branch January 14, 2025 03:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants